package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AgentDepositVip;
import com.tbit.uqbike.service.business.AgentDepositVipService;
import com.tbit.uqbike.webmanager.dao.core.AgentDepositVipDao;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 用户区域免押
 * @Date: 创建于 17:46 2020/6/16
 */
@Service
public class AgentDepositVipServiceImpl implements AgentDepositVipService {
    @Autowired
    private AgentDepositVipDao agentDepositVipDao;

    @Override
    public void insert(AgentDepositVip agentDepositVip) {
        AgentDepositVip agentDepositVipDB = getByUserId(agentDepositVip.getUserId(), agentDepositVip.getAccountId());
        if (agentDepositVipDB != null) {
            throw new BaseException("ver.userMenBer.exist");
        }
        agentDepositVip.setLastUpdateDt(DateTimeUtil.getNowTime());
        agentDepositVipDao.insert(agentDepositVip);
    }

    @Override
    public AgentDepositVip getByUserId(Integer userId, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("accountId", accountId);
        return agentDepositVipDao.getByUserId(map);
    }

    @Override
    public void edit(AgentDepositVip agentDepositVip) {
        agentDepositVip.setLastUpdateDt(DateTimeUtil.getNowTime());
        AgentDepositVip agentDepositVipDB = getByUserId(agentDepositVip.getUserId(), agentDepositVip.getAccountId());
        if (agentDepositVipDB != null) {
            agentDepositVipDao.edit(agentDepositVip);
        } else {
            agentDepositVipDao.insert(agentDepositVip);
        }
    }

    @Override
    public void del(Integer userId, Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("accountId", accountId);
        agentDepositVipDao.del(map);
    }

    @Override
    public List<AgentDepositVip> queryPage(Integer accountId, String phone, Integer pageNO, Integer rowCount) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("phone", phone);
        params.put("pageNO", (pageNO - 1) * rowCount);
        params.put("rowCount", rowCount);
        return agentDepositVipDao.queryPage(params);
    }

    @Override
    public Integer queryCount(Integer accountId, String phone) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("accountId", accountId);
        params.put("phone", phone);
        return agentDepositVipDao.queryCount(params);
    }

    @Override
    public AgentDepositVip selectByAccountIdList(Integer userId, List<Integer> accountIdList) {
        return agentDepositVipDao.selectByAccountIdList(userId, accountIdList);
    }
}
